
$(function () {

    initTable();
    loadRoleData();

    $("#roleadd").on("click",function(){
        addRole();
    })
})

function initTable() {
    $("#role").bootstrapTable({
        height: 'auto',
        pagination:true,
        pageSize: 10,
        pageList: [5,10,20],
        columns: [
            {
                title:'序号',
                field: 'roleid',
                //checkbox: true,
                align: 'center',
                valign: 'middle'
            }, {
                title: '角色序号',
                field: 'ordernum',
                align: 'left',
                valign: 'middle',
                sortable: true
            }, {
                title: '名称',
                field:'rolename',
                align: 'center'
            }, {
                field:'roledesc',
                title: '角色描述',
                align: 'center'
            }, {
                title: '操作',
                field: '',
                width: '20%',
                formatter: function (value, row, index) {
                    var html = [];
                    html.push('<button class="btn btn-warning btn-xs" style="margin: 0px 5px;">修改</button>');
                    html.push('<button class="btn btn-danger btn-xs" style="margin: 0px 5px;">删除</button>');
                    return html.join('');
                },
                events: {
                    'click .btn-warning': function (e, value, row, index) {
                        updateRole(row);

                    },
                    'click .btn-danger': function (e, value, row, index) {
                        deleteRole(row['roleid']);
                    }

                }
            }
        ],
        onDblClickRow:function (row, $element, field) {
            
        }
    });
}

/**
 * 加载角色数据
 */
function loadRoleData() {
    var url = "../roleManage/selectAll";
    var index= layer.load(1);
    $.getJSON(url,"",function (data) {
        layer.close(index);
        $("#role").bootstrapTable("load",data);
    })
}

/**
 * 弹出用户表格
 * @param data
 */
function addRole(data) {
    var html = [];
    html.push('<div id="newRole">' +'<input type="hidden" id="new_roleid">'+
        '<table class="sitetable table table-bordered">');
    // html.push('<thead><tr><td style="width: 150px;">角色列表</td></tr></thead>');
    // html.push('<tr><td style="width: 150px;">角色：</td><td><select class="form-control" id="userright">');
    // html.push('</select></td></tr>');
    html.push('<tr><td>角色名称：</td><td><input type="text" class="form-control" id="new_rolename" validateVal="0"/></td></tr>');
    html.push('<tr><td>角色描述：</td><td><input type="text" class="form-control" id="new_roledesc" validateVal="0"/></td></tr>');
    html.push('<tr><td>序号：</td><td><input type="text" class="form-control" id="new_ordernum" validateVal="1" /></td></tr>');
    html.push('</table></div>');

    layer.open({
        type: 1,
        title: '添加角色',
        btn: ['OK'],//可以无限个按钮
        content: html.join(''),
        success: function (layero, index) {
            
        },
        yes:function (index) {
            var p = {};
            p.roleid = $("#roleid").val();
            p.rolename = $("#new_rolename").val();
            p.roledesc = $("#new_roledesc").val();
            p.ordernum = $("#new_ordernum").val();
            layer.close(index);
            var url = "../roleManage/insert";
            $.post(url,p,function (data) {
                if(data===true){
                    loadRoleData();
                    layer.msg("添加成功！",{icon: 1});
                }else{
                    layer.msg("添加失败！",{icon: 2});
                }
            })
        }
    });


}

function updateRole(data) {
   // console.log(data);
    var html = [];
    html.push('<div id="newRole">' +'<input type="hidden" id="new_roleid">'+
        '<table class="sitetable table table-bordered">');
    // html.push('<thead><tr><td style="width: 150px;">角色列表</td></tr></thead>');
    // html.push('<tr><td style="width: 150px;">角色：</td><td><select class="form-control" id="userright">');
    // html.push('</select></td></tr>');
    html.push('<tr><td>角色名称：</td><td><input type="text" class="form-control" id="new_rolename" validateVal="0"/></td></tr>');
    html.push('<tr><td>角色描述：</td><td><input type="text" class="form-control" id="new_roledesc" validateVal="0"/></td></tr>');
    html.push('<tr><td>序号：</td><td><input type="text" class="form-control" id="new_ordernum" validateVal="1" /></td></tr>');
    html.push('</table></div>');

    layer.open({
        type: 1,
        title: '修改角色',
        btn: ['OK'],//可以无限个按钮
        content: html.join(''),
        success: function (layero, index) {
            $("#new_roleid").val(data["roleid"]);
            $("#new_rolename").val(data["rolename"]);
            $("#new_roledesc").val(data["roledesc"]);
            $("#new_ordernum").val(data["ordernum"]);
        },
        yes:function (index) {
            var p = {};
            p.roleid = $("#new_roleid").val();
            p.rolename = $("#new_rolename").val();
            p.roledesc = $("#new_roledesc").val();
            p.ordernum = $("#new_ordernum").val();
            p.isduty = "";
            layer.close(index);
            console.log(p);
            var url = "../roleManage/update";

          $.post(url,p,function (data) {
                if (data === true) {
                    loadRoleData();
                    layer.msg("修改成功！", {icon: 2});
                } else {
                    layer.msg("修改失败！", {icon: 2});
                }
            });
            /*$.ajax({
                url : "/roleManage/update",
                type : "POST",
                contentType : "application/json",
                dataType : "json",
                data : JSON.stringify(p),//转换成字符串。后台使用了RequestBody注解
                success : function(data) {
                    if (data === true) {
                        loadRoleData();
                        layer.msg("修改成功！", {icon: 1});
                    } else {
                        layer.msg("修改失败！", {icon: 2});
                    }
                },
                error :function(){
                    alert("添加失败");
                }
            });*/
        }
    });
}

function  deleteRole(roleid) {
    var url = "../roleManage/delete";
    $.post(url,{"roleid":roleid},function (data) {
        if(data === true){
            layer.msg("删除成功！");
            loadRoleData();
        }else{
            layer.msg("删除失败！");
        }
    })
}